package com.cxj.musicplayer.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.cxj.musicplayer.bean.Song;

import java.util.ArrayList;
import java.util.List;

public class SongDBUtil extends SQLiteOpenHelper {
    public SongDBUtil(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public SongDBUtil(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public SongDBUtil(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, name, version, openParams);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table song(" +
                " id Integer primary key autoincrement," +
                " song_id text, " +
                " love text, " +
                " song_name text, " +
                " author text);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public Song querySong(String song_id){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from song where song_id = ?", new String[]{song_id});
        if (cursor.getCount() != 0) {
            cursor.moveToNext();
            String song_name = cursor.getString(3);
            String love = cursor.getString(2);
            String author = cursor.getString(4);
            Song song = new Song();
            song.setSong_name(song_name);
            song.setAuthor(author);
            song.setSong_id(song_id);
            song.setLove(love);
            cursor.close();
            return song;
        } else {
            return null;
        }

    }

    public boolean insertSong(Song song){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("song_id", song.getSong_id());
        contentValues.put("love", song.getLove());
        contentValues.put("song_name", song.getSong_name());
        contentValues.put("author", song.getAuthor());
        db.insert("song", null, contentValues);
        return true;
    }

    public boolean updateSong(Song song){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("song_id", song.getSong_id());
        contentValues.put("love", song.getLove());
        contentValues.put("song_name", song.getSong_name());
        contentValues.put("author", song.getAuthor());
        db.update("song", contentValues, " song_id = ? ", new String[]{song.getSong_id()});
        return true;
    }

    public boolean deleteSong(String song_id){
        SQLiteDatabase db = getWritableDatabase();
        db.delete("song", " song_id = ? ", new String[]{song_id});
        return true;
    }

    public List<Song> queryAllSongs(){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from song where love = 'yes'", null);
        List<Song> res = new ArrayList<>();
        if(cursor.getCount() > 0){
            while (cursor.moveToNext()){
                String song_name = cursor.getString(3);
                String love = cursor.getString(2);
                String author = cursor.getString(4);
                String song_id = cursor.getString(1);
                Song song = new Song();
                song.setSong_name(song_name);
                song.setAuthor(author);
                song.setSong_id(song_id);
                song.setLove(love);
                res.add(song);
            }
            return res;
        }
        return null;
    }

    public int getLoveCount(){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from song where love = 'yes'", null);
        return cursor.getCount();
    }
}
